package com.mz.ozy.mvp.presenter;


import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;


import com.mz.ozy.base.BasePresenterImpl;
import com.mz.ozy.mvp.model.RegisterModel;
import com.mz.ozy.mvp.view.RegisterContract;
import com.mz.ozy.net.MyObserver;
import com.mz.ozy.utils.RxUtil;

import org.json.JSONObject;

import java.io.InputStream;

import okhttp3.ResponseBody;

/**
 * Created by administrator on 2018/12/3.
 */
public class RegisterPresenter extends BasePresenterImpl<RegisterContract.View> implements RegisterContract.Presenter {

    private RegisterModel mRegisterModel;

    public RegisterPresenter() {
        mRegisterModel = new RegisterModel();
    }


    @Override
    public void obtainRegister(Activity activity, String uname, String userName, String code, String password, String paypwd, String tjr) {
        mRegisterModel.onRegister(activity, uname, userName, code, password, paypwd, tjr)
                .compose(RxUtil.rxSchedulerHelper(mView.<ResponseBody>bindToLifecycle()))
                .subscribe(new MyObserver<JSONObject>(mView) {
                    @Override
                    public void onSuccesss(JSONObject jsonObject) {
                        mView.obtainRegisterSuccess(jsonObject);
                    }
                });
    }


    @Override
    public void getCode(Activity activity, String mobilePhone, String ivCode) {
        mRegisterModel.getCode(activity, mobilePhone, ivCode)
                .compose(RxUtil.rxSchedulerHelper(mView.<ResponseBody>bindToLifecycle()))
                .subscribe(new MyObserver<JSONObject>(mView) {
                    @Override
                    public void onSuccesss(JSONObject jsonObject) {
                        mView.getCodeSuccess(jsonObject);
                    }
                });
    }

    @Override
    public void getImageCode(Context context) {
        mRegisterModel.getCaptcha(context)
                .compose(RxUtil.rxSchedulerHelper(mView.<ResponseBody>bindToLifecycle()))
                .subscribe(new MyObserver<ResponseBody>(mView) {

                    @Override
                    public void onSuccesss(ResponseBody responseBody) {

                    }

                    @Override
                    public void onNext(ResponseBody responseBody) {
                        InputStream inputStream = responseBody.byteStream();//得到图片流
                        Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
                        if (bitmap != null && mView != null) {
                            mView.getImageCodeInfo(bitmap);
                        }
                    }

                });
    }
}
